import $config from '@/utils/config/config.js'
import $constant from '@/utils/constant/constant.js'
import $fn from "@/utils/functions/functions.js"
import mixins from '@/utils/mixins/mixins.js'

// application/json application/x-www-form-urlencoded 

const request = (opts) => {
	let header = opts.header || {};
	header['Content-Type'] = 'application/x-www-form-urlencoded'
	header['token'] = uni.getStorageSync('company_token') || ''
	let data = {
		...opts.data,
		// ...requestFrom()
	} || {}
	data.token = uni.getStorageSync('company_token') || ''
	delete data['fail']
	delete data['complete']
	delete data['loading']
	let fail = opts.data?.fail || null
	let complete = opts.data?.complete || null
	let loading = opts.data?.loading || false
	if (loading) {
		uni.showLoading({
			title: '加载中'
		})
	}
	// let baseUrl = location.protocol + '//yunying.zanzanyun.cn/qwapi/'
	let baseUrl
	if (process.env.NODE_ENV === 'development') {
		baseUrl = $config.baseUrl
	} else {
		baseUrl = `${location.origin}/qwapi/`
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + opts.url,
			method: opts.method || 'POST',
			timeout: $config.pingInterval,
			data,
			header,
			success: (response) => {
				const res = response.data
				let err = {
					title: '请求失败,请联系平台',
					icon: 'none',
					duration: 3000,
					code: response.statusCode == 200 ? res.code : response.statusCode,
					data: response.data,
					url: opts.url,
					param: JSON.stringify(data)
				}
				if (response.statusCode != 200) {
					response.statusCode == 404 ? err.title = '请求失败,请检查网络' : ''
					response.statusCode == 500 ? err.title = '请求失败,服务器未响应' : ''
					typeof fail == 'function' && fail(err)
					reject(err)
					msg(err)
					alert(opts.url)
					return false
				}
				let code = Number(res.code)
				switch (res.code) {
					case 1: // 请求成功
						resolve(res.data)
						break
					case 0: // 请求错误
						err.title = res.msg
						typeof fail == 'function' && fail(err)
						reject(err)
						msg(err)
						break
					case -1: // token失效
						err.title = '登录过期，请登录后操作'
						typeof fail == 'function' && fail(err)
						reject(err)
						msg(err)
						uni.removeStorageSync('company_token')
						uni.removeStorageSync('companyUserInfo')
						uni.removeStorageSync('cid')
						uni.setStorage({
							key: 'loginAgain',
							data: location.href,
							success: function() {
								setTimeout(() => {
									uni.navigateTo({
										url: '/pages/login/login'
									})
								}, 300)
							}
						});
					default:
						err.title = res.msg
						typeof fail == 'function' && fail(err)
						reject(err)
						msg(err)
				}
			},
			fail: (err) => {
				typeof fail == 'function' && fail(err)
				reject(err)
				msg(err)
			},
			complete: (res) => {
				if (loading) {
					uni.hideLoading()
				}
				typeof complete == 'function' && complete(res)
			}
		})
	})
}
const msg = (err) => {
	console.log(`=========接口报错--开始=========`)
	console.log(`接口地址=====>${err.url}`)
	console.log(`接口传参=====>${err.param}`)
	console.log(`=========接口报错--结束=========`)
	uni.showToast({
		title: err.title || '请求失败,请检查网络',
		icon: 'none',
		duration: 3000
	})
}
const requestFrom = () => {
	// #ifdef H5
	return {
		app_type: $fn.isWeiXin() ? 'wechat' : 'h5',
		app_type_name: $fn.isWeiXin() ? '微信公众号' : 'H5',
	}
	// #endif
	// #ifdef MP-WEIXIN
	return {
		app_type: 'weapp',
		app_type_name: '微信小程序'
	}
	// #endif
	// #ifdef APP-PLUS
	return {
		app_type: 'app',
		app_type_name: 'app'
	}
	// #endif
	// #ifdef MP-ALIPAY
	return {
		app_type: 'alipay',
		app_type_name: '支付宝小程序'
	}
	// #endif
	// #ifdef MP-BAIDU
	return {
		app_type: 'baidu',
		app_type_name: '百度小程序'
	}
	// #endif
	// #ifdef MP-TOUTIAO
	return {
		app_type: 'toutiao',
		app_type_name: '抖音小程序'
	}
	// #endif
}
export default request